package server.qiang.Process;

import common.harvin.potocol.Request;
import common.harvin.potocol.Response;
import server.qiang.database.database;
import server.qiang.email.Checkmail;

import java.sql.SQLException;

public class Register {
	//private OutputThread out;
	//private OutputThreadMap map;

	public Response register(Request request) {
		System.out.println("command:" + request.getCommand());
		System.out.println("sender:" + request.getSender());
		System.out.println("data:" + request.getData("email"));
		System.out.println("data:" + request.getData("password"));
		System.out.println("send_Date:" + request.getSend_Date());


		Response response = new Response();
		response.setFor_command(request.getCommand());
		response.setSend_Date(request.getSend_Date());

		String emailserver = null;
		String emailhost = null;
		String email = request.getData("email");
		emailserver = email.substring(email.indexOf("@") + 1, email.indexOf("."));
		emailhost = "smtp." + emailserver + ".com";

		try {
			database base = new database();

			if (base.IsUser(request.getSender()) == false) {
				Checkmail checkmail = new Checkmail();


				if (checkmail.sendcheckMail(emailhost, request.getData("email"), request.getData("password"))) {
					base.InsertUser(request.getSender(), request.getData("email"), request.getData("password"));
					response.setResult(true);
					System.out.println("用户注册成功");
					response.addData("response", "用户注册成功");
				} else {
					System.out.println("邮箱或密码不正确");
					response.addData("response", "邮箱或密码不正确");
				}


			} else {
				System.out.println("用户已注册过");
				//response.setResult(true);
				response.addData("response", "用户已注册过");

			}


		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("数据库有错误");
			response.addData("response", "数据库有错误");
		}


		return response;
	}

}
